package org.skydiveapphd;

import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapphd.interfaces.NotesNotifier;
import org.skydiveapplibrary.data.Aircraft;
import org.skydiveapplibrary.repository.AircraftRepository;
import org.skydiveapplibrary.R;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class AircraftEditActivity extends Fragment implements NotesNotifier
{
	// repository
	private AircraftRepository repository;
	
	// data
	private boolean isEdit = false;
	private Aircraft aircraft;
	
	// listeners
	private OnClickListener saveListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			save();
		}
	};
	private OnClickListener cancelListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			getFragmentManager().popBackStack();
		}
	};
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			commitChanges();
			
			Bundle args = new Bundle();
			args.putString(Constants.EXTRA_FRAG_TAG, AircraftEditActivity.this.getTag());
			args.putString(Constants.EXTRA_NOTES, aircraft.getNotes());
			
			FragmentHelper.getInstance().openFragment(NotesEditActivity.newInstance(args), android.R.id.tabhost, 
					Constants.TAG_NOTES_EDIT, getFragmentManager());
		}
	};
	private OnClickListener deleteListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			showDeleteWarning();
		}
	};
	private DialogInterface.OnClickListener deleteConfirmListener = new DialogInterface.OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			delete();
		}
	};
	
	public static AircraftEditActivity newInstance(Bundle args) 
	{
		AircraftEditActivity f = new AircraftEditActivity();
		f.setArguments(args);
		return f;
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// load data
		repository = new AircraftRepository(getActivity());
		loadData();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    		Bundle savedInstanceState)
	{	
	    // Inflate the layout for this fragment
	    return inflater.inflate(R.layout.aircraft_edit_view, container, false);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		initializeUI();
	}
	
	private void loadData()
	{
		// set isEdit flag
		isEdit = getArguments().containsKey(Constants.EXTRA_AIRCRAFT_ID);

		// determine add vs. edit
		if (isEdit)
		{
			// editing
			int id = getArguments().getInt(Constants.EXTRA_AIRCRAFT_ID);
			aircraft = repository.getAircraft(id);
		}
		else
		{
			// adding
			aircraft = new Aircraft();
		}
	}
	
	private void initializeUI()
	{
		// set header text
		TextView headerTextView = (TextView) getActivity().findViewById(R.id.EditHeaderTextView);
		headerTextView.setText(isEdit ?
				R.string.aircraft_edit_label : R.string.aircraft_new_label);
		
		// set name
		EditText nField = (EditText) getActivity().findViewById(R.id.AircraftNameField);
		nField.setText(aircraft.getName());
		
		// set notes
		TextView notesField = (TextView) getActivity().findViewById(R.id.AircraftNotesField);
		notesField.setText(aircraft.getNotes());
		notesField.setOnClickListener(notesEditListener);

		// add button listeners
		ImageButton saveButton = (ImageButton) getActivity().findViewById(R.id.AircraftSaveButton);
		saveButton.setOnClickListener(saveListener);
		
		ImageButton cancelButton = (ImageButton) getActivity().findViewById(R.id.AircraftCancelButton);
		cancelButton.setOnClickListener(cancelListener);
		
		ImageButton deleteButton = (ImageButton) getActivity().findViewById(R.id.AircraftDeleteButton);
		deleteButton.setOnClickListener(deleteListener);
		
		// show/hide delete button
		deleteButton.setVisibility(isEdit ? View.VISIBLE : View.GONE);
	}
	
	private void showDeleteWarning()
	{
		// create alert dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setMessage(R.string.aircraft_delete_warning);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.delete_confirmation);
		builder.setPositiveButton(R.string.yes_button, deleteConfirmListener);
		builder.setNegativeButton(R.string.no_button, null);
		AlertDialog dialog = builder.create();
		// show dialog
		dialog.show();
	}
	
	public void updateNotes(String notes)
	{
		// update aircraft
		aircraft.setNotes(notes);
	}

	private void commitChanges()
	{
		// set name
		EditText nField = (EditText) getActivity().findViewById(R.id.AircraftNameField);
		String name = nField.getText().toString();
		aircraft.setName(name);
	}
	
	private void save()
	{
		commitChanges();
		
		// save
		if (isEdit)
		{
			repository.updateAircraft(aircraft);
		}
		else
		{
			repository.addAircraft(aircraft);
		}
		
		// finish
		getFragmentManager().popBackStack();
	}
	
	private void delete()
	{
		// delete aircraft
		repository.deleteAircraft(aircraft);
		
		// finish
		getFragmentManager().popBackStack();
	}
}
